<%@ page language="java" import="util.*, java.sql.*, java.util.*" %>
<html><head>
    <Title>Search Page</Title>
</head>
    <%
        String msg = util.Utilities.isCorrectStaffLogin(session);
        String name = (String)session.getAttribute("username");
        if(msg != null) {
            out.println(msg);
        }else {
    %>
    <body>
        <center><h3>Search Result</h3></center>
        <table align="center" border="1">
            <tr bgcolor="white"><b>
                <td>PaymentID</td>
                <td>Cardholder name:</td>
                <td>CardNumber</td>
                <td>Bank</td>
                <td>Date</td>
                <td>Company</td>
                <td>Amount</td>
                <td>Date</td>
                <td>Time</td>
            </tr>
            <%
                Connection con = null;
                int totalRow=0;
                try {
                    int rowsPerPage = 10;
                    int rowCount = 0;
                    boolean more = false;


                    int nextRow = 0;
                    String next = request.getParameter("previous");
                    if (Utilities.isValid(next)) {
                        nextRow = Integer.parseInt(next);
                    }
                    String start = request.getParameter("start");
                    String end1 = request.getParameter("end");
                    con = ConnectionManager.connect();
                    String query = ("select * from payment WHERE date >= #"+start+"# AND date <=#"+end1+"#");
                    Statement stmt = con.createStatement();
                    ResultSet rs = stmt.executeQuery(query);
                    con.commit();

                    for (int i=0; i<nextRow; i++) {
                        rs.next();
                    }

                    while(rs.next()) {
                        rowCount++;

                        String holdername = rs.getString("holdername");
                        String number = rs.getString("number");
                        String bank = rs.getString("bank");
                        String issueDate = rs.getString("issueDate");
                        String company = rs.getString("company");
                        String amount = rs.getString("amount");
                        String paymentID = rs.getString("paymentID");
                        java.sql.Date date = rs.getDate("date");
                        java.sql.Time time = rs.getTime("time");
            %>
            <form action='DeletePayment.jsp' method='get'>
                <tr>
                    <td>
                        <%=paymentID%>
                        <input type='hidden' name='paymentID' value='<%=paymentID%>' />
                    </td>
                    <td>
                        <%=holdername%>
                        <input type='hidden' name='holdername' value='<%=holdername%>' />
                    </td>
                    <td>
                        <%=number%>
                        <input type='hidden' name='number' value='<%=number%>' />
                    </td>
                    <td>
                        <%=bank%>
                        <input type='hidden' name='bank' value='<%=bank%>' />
                    </td>
                    <td>
                        <%=issueDate%>
                        <input type='hidden' name='issueDate' value='<%=issueDate%>' />
                    </td>
                    <td>
                        <%=company%>
                        <input type='hidden' name='company' value='<%=company%>' />
                    </td>
                    <td>
                    $<%=amount%>
                    <input type='hidden' name='amount' value='<%=amount%>' />
                    <td>
                        <%=date%>
                        <input type='hidden' name='date' value='<%=date%>' />
                    </td>
                    <td>
                        <%=time%>
                        <input type='hidden' name='company' value='<%=time%>' />
                    </td>
                    <td valign='top'>
                        <input type='Submit' value='Delete'>
                    </td>        
                </tr>
            </form>
            <%
                if ((rowsPerPage > 0) && (rowCount >= rowsPerPage)) {
                    // Find out if there are any more rows after this one
                    more = rs.next();
                    break;
                }
                }
            %>
        </table>
        <%
            if (more) {
        %>
        <form method='get' action='DisplayPayment.jsp'>
            <center>
                <input type='hidden' name='previous' value='<%=rowCount+nextRow%>' />
                <%   
                    int nextValue = 5;
                    int remain = totalRow-(rowCount+nextRow);
                    if (remain < rowsPerPage) {
                        nextValue = remain % rowsPerPage;
                    }

                    int page2=0;
                    String page1 = request.getParameter("page");
                    try {
                        if(Utilities.isValid(page1)) {
                            page2 = Integer.parseInt(page1);
                        }
                    }catch (Exception e) {
                        out.println(e);
                    }
                    int begin=1,end=10;
                    int totalPage = (int)Math.ceil(totalRow/rowsPerPage);

                    if(page2<7) {

                    } else if(page2>totalPage-5) {
                        begin = totalPage - 9;
                        end = totalPage;
                    } else {
                        begin = page2 -5;
                        end = page2 + 4;
                    }



                    int j=begin;
                    for (int i=begin; i<=end; i++) {
                %>
                <a href='byType.jsp?page=<%=j%>&amp;previous=<%=i*rowsPerPage%>'><%=++j%></a>
                <%
                    }
                %>
                <input type='hidden' name='page' value='<%=j%>' />
                <input type = submit value='Next <%=nextValue%> rows'/>
            </center>
        </form>
        <%
            }
                }catch (Exception e) {
                    out.println(e);
                }finally {
                    con.close();
                }
        }
        %>
   
    
    </body>
</html>